/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.posting;

import domain.DomainController;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class BBCodePanel extends javax.swing.JPanel
{
    private String[] bold = new String[]{"<html><strong>B</strong></html>", "<html><strong>B</strong> *</html>"};
    private String[] italic = new String[]{"<html><i>i</i></html>", "<html><i>i</i> *</html>"};
    private String[] underline = new String[]{"<html><u>u</u></html>", "<html><u>u</u> *</html>"};

    /** Creates new form BBCodePanel */
    public BBCodePanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdBold = new javax.swing.JButton();
        cmdItalic = new javax.swing.JButton();
        cmdUnderline = new javax.swing.JButton();
        cmdQuote = new javax.swing.JButton();
        cmdCode = new javax.swing.JButton();
        cmdImg = new javax.swing.JButton();

        setLayout(new java.awt.GridLayout(1, 6));

        cmdBold.setText("<html><strong>B</strong></html>");
        cmdBold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBoldActionPerformed(evt);
            }
        });
        add(cmdBold);

        cmdItalic.setText("<html><i>i</i></html>");
        cmdItalic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdItalicActionPerformed(evt);
            }
        });
        add(cmdItalic);

        cmdUnderline.setText("<html><u>u</u></html>");
        cmdUnderline.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdUnderlineActionPerformed(evt);
            }
        });
        add(cmdUnderline);

        cmdQuote.setText("Quote");
        cmdQuote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdQuoteActionPerformed(evt);
            }
        });
        add(cmdQuote);

        cmdCode.setText("Code");
        cmdCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCodeActionPerformed(evt);
            }
        });
        add(cmdCode);

        cmdImg.setText("Img");
        cmdImg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdImgActionPerformed(evt);
            }
        });
        add(cmdImg);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdBoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBoldActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdBold.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/b]");
                this.cmdBold.setText(bold[0]);
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[b]");
                this.cmdBold.setText(bold[1]);
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[b]" + text + "[/b]");
        }

    }//GEN-LAST:event_cmdBoldActionPerformed

    private void cmdItalicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdItalicActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdItalic.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/i]");
                this.cmdItalic.setText(italic[0]);
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[i]");
                this.cmdItalic.setText(italic[1]);
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[i]" + text + "[/i]");
        }

    }//GEN-LAST:event_cmdItalicActionPerformed

    private void cmdUnderlineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUnderlineActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdUnderline.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/u]");
                this.cmdUnderline.setText(underline[0]);
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[u]");
                this.cmdUnderline.setText(underline[1]);
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[u]" + text + "[/u]");
        }

    }//GEN-LAST:event_cmdUnderlineActionPerformed

    private void cmdQuoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdQuoteActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdQuote.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/quote]");
                this.cmdQuote.setText(this.cmdQuote.getText().replace(" *", ""));
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[quote]");
                this.cmdQuote.setText(this.cmdQuote.getText() + " *");
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[quote]" + text + "[/quote]");
        }

    }//GEN-LAST:event_cmdQuoteActionPerformed

    private void cmdCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCodeActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdCode.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/code]");
                this.cmdCode.setText(this.cmdCode.getText().replace(" *", ""));
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[code]");
                this.cmdCode.setText(this.cmdCode.getText() + " *");
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[code]" + text + "[/code]");
        }

    }//GEN-LAST:event_cmdCodeActionPerformed

    private void cmdImgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdImgActionPerformed

        String text = DomainController.getInstance().getSelectedTextInPost();

        if(text == null)
        {
            if(this.cmdImg.getText().contains(" *"))
            {
                //Insert end tag.
                DomainController.getInstance().insertTagInPostBody("[/img]");
                this.cmdImg.setText(this.cmdImg.getText().replace(" *", ""));
            }
            else
            {
                //Inset begin tag.
                DomainController.getInstance().insertTagInPostBody("[img]");
                this.cmdImg.setText(this.cmdImg.getText() + " *");
            }
        }
        else
        {
            //Text selected...
            DomainController.getInstance().insertTagInPostBody("[img]" + text + "[/img]");
        }

    }//GEN-LAST:event_cmdImgActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdBold;
    private javax.swing.JButton cmdCode;
    private javax.swing.JButton cmdImg;
    private javax.swing.JButton cmdItalic;
    private javax.swing.JButton cmdQuote;
    private javax.swing.JButton cmdUnderline;
    // End of variables declaration//GEN-END:variables

    public void setButtonsEnabled(boolean enabled)
    {
        this.cmdBold.setEnabled(enabled);
        this.cmdCode.setEnabled(enabled);
        this.cmdImg.setEnabled(enabled);
        this.cmdItalic.setEnabled(enabled);
        this.cmdQuote.setEnabled(enabled);
        this.cmdUnderline.setEnabled(enabled);
    }

    public void clearPost()
    {
        this.cmdBold.setText(bold[0]);
        this.cmdItalic.setText(italic[0]);
        this.cmdUnderline.setText(underline[0]);
        this.cmdQuote.setText(this.cmdQuote.getText().replace(" *", ""));
        this.cmdCode.setText(this.cmdCode.getText().replace(" *", ""));
        this.cmdImg.setText(this.cmdImg.getText().replace(" *", ""));
    }
}